package com.gc.baselib.core;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.gc.baselib.BuildConfig;

public class CoreApi {
    private Context mContext;


    private boolean isAppInit = false;


    private static CoreApi mCoreApi = new CoreApi();

    public String getVersionName() {
        return versionName;
    }

    public int getVersionCode() {
        return versionCode;
    }

    private String versionName;
    private int versionCode = 1;

    public static CoreApi getInstance() {
        return mCoreApi;
    }


    public boolean isUseMock() {
        return useMock;
    }

    public void setUseMock(boolean useMock) {
        this.useMock = useMock;
    }

    private boolean useMock = false;


    public Context getContext() {
        return mContext;
    }

    public Handler getHandler() {
        return mHandler;
    }

    public boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }

    public boolean isEnableWriteLog() {
        return CoreContants.isEnableWrite;
    }

    public boolean isEnableUploadLog() {
        return CoreContants.isEnableUpload;
    }

    public boolean isPerfermanceWatcher() {
        return CoreContants.isPerfermanceWatcher;
    }

    //    private Config config;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    public void init(Context context) {
        this.mContext = context;
        getAndroiodScreenProperty();
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void getAndroiodScreenProperty() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)


        Log.e("CoreApi", "屏幕宽度（像素）：" + width);
        Log.e("CoreApi", "屏幕高度（像素）：" + height);
        Log.e("CoreApi", "屏幕密度（0.75 / 1.0 / 1.5）：" + density);
        Log.e("CoreApi", "屏幕密度dpi（120 / 160 / 240）：" + densityDpi);
        Log.e("CoreApi", "屏幕宽度（dp）：" + screenWidth);
        Log.e("CoreApi", "屏幕高度（dp）：" + screenHeight);
    }


    public void setAppInit(boolean init) {
        this.isAppInit = init;
    }

    public boolean isAppInit() {
        return isAppInit;
    }

    private static class CoreContants {//开发专用
        private static final boolean isEnableWrite = false;
        private static final boolean isEnableUpload = false;
        private static final boolean isPerfermanceWatcher = true;
    }


//    public String getNodataStr() {
//        return  config.getNoDataStr();
//    }
//
//    public String getNoNetworkStr() {
//        return  config.getNoNetWorkStr();
//    }
//
//    public int getCommonNoDataImgId() {
//        return  config.getNoDataImgId();
//    }
//
//    public int getCommonNoNetworkImgId() {
//        return  config.getNoNetworkImgId();
//    }


//    private static class Config{
//        private String noDataStr;
//        private String noNetWorkStr;
//        private int noDataImgId;
//        private int noNetworkImgId;
//
//        public static Config createDefaultConfig() {
//            Config config=new Config()
//                .setNoDataImgId(R.mipmap.app_back_icon)
//                .setNoDataStr("无数据")
//                .setNoNetWorkStr("无网络")
//                .setNoNetworkImgId(R.mipmap.app_back_icon);
//            return config;
//        }
//
//        public String getNoDataStr() {
//            return noDataStr;
//        }
//
//        public Config setNoDataStr(String noDataStr) {
//            this.noDataStr = noDataStr;
//            return this;
//        }
//
//        public String getNoNetWorkStr() {
//            return noNetWorkStr;
//        }
//
//        public Config setNoNetWorkStr(String noNetWorkStr) {
//            this.noNetWorkStr = noNetWorkStr;
//            return this;
//        }
//
//        public int getNoDataImgId() {
//            return noDataImgId;
//        }
//
//        public Config setNoDataImgId(int noDataImgId) {
//            this.noDataImgId = noDataImgId;
//            return this;
//        }
//
//        public int getNoNetworkImgId() {
//            return noNetworkImgId;
//        }
//
//        public Config setNoNetworkImgId(int noNetworkImgId) {
//            this.noNetworkImgId = noNetworkImgId;
//            return this;
//        }
//    }
}
